package com.gongpingjialoan.activity.car;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseScrollFragment;
import com.gongpingjialoan.utility.Utils;
import com.gongpingjialoan.widget.LoadingDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class NewCarParametsFragment extends BaseScrollFragment {

    private View view;

    private LayoutInflater layoutInflater;


    private LinearLayout new_car_details, new_car_div;

    private ImageView no_new_info;

    private String data;

    private LoadingDialog loadingDialog;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable
            ViewGroup container, @Nullable Bundle savedInstanceState) {
        isScrollPage = true;
        this.layoutInflater = inflater;
        view = inflater.inflate(R.layout.include_newcar_layout, container, false);
        initView();
        Bundle bundle = getArguments();
        if (bundle != null) {
            data = bundle.getString("json_data", "");
            if (!TextUtils.isEmpty(data)) {
                loadingDialog.show();
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        JSONObject ob = null;
                        try {
                            ob = new JSONObject(data);
                            bindNewCarView(ob);
                            loadingDialog.dismiss();
                        } catch (JSONException e) {
                            loadingDialog.dismiss();
                        }
                    }
                }, 100);
                //

            }
        }


        return view;
    }


    private void initView() {
        loadingDialog = new LoadingDialog(getActivity());
        new_car_div = (LinearLayout) view.findViewById(R.id.new_car_div);
        new_car_div.setVisibility(View.VISIBLE);
        new_car_details = (LinearLayout) view.findViewById(R.id.new_car_details);
        no_new_info = (ImageView) view.findViewById(R.id.no_new_info);
    }

    public void loadDataSuccess(JSONObject json) {
        if (!TextUtils.isEmpty(data)) {
            return;
        }
        bindNewCarView(json);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    /**
     * 绑定新城配置视图
     */
    private void bindNewCarView(JSONObject json) {
        try {
            View titleView;
            View descView;
            TextView txt;
            TextView title_txt;
            TextView desc_txt;
            //JSONObject jsonProduct = json.getJSONObject("car_data");
            List<View> list = new ArrayList<View>();
            JSONArray paramsArray = json.getJSONArray("params");
            Bundle bundle = getArguments();
            String from = "";
            if (bundle != null) {
                from = bundle.getString("from", "");
            }
            //为空代表从车源详情过来的
            JSONArray configsArray;
            if (TextUtils.isEmpty(from)) {
                configsArray = json.getJSONArray("configs");
            } else {
                //从新车详情过来的
                configsArray = json.getJSONArray("config");
            }
            for (int i = 0; i < paramsArray.length(); i++) {
                List<View> descList = new ArrayList<View>();
                JSONObject pJson = paramsArray.getJSONObject(i);
                Iterator<?> keys = pJson.keys();
                while (keys.hasNext()) {
                    String key = (String) keys.next();
                    String value = pJson.get(key).toString();
                    if ("null".equals(value)) {
                        value = "";
                    }
                    if (key.equals("para_cat")) {
                        titleView = layoutInflater.inflate(R.layout.car_details_new_item_title, null);
                        txt = (TextView) titleView.findViewById(R.id.txt);
                        txt.setText(value);
                        list.add(titleView);
                    } else {
                        descView = layoutInflater.inflate(R.layout.car_details_new_item_desc, null);
                        title_txt = (TextView) descView.findViewById(R.id.title_txt);
                        desc_txt = (TextView) descView.findViewById(R.id.desc_txt);

                        title_txt.setText(key);
                        desc_txt.setText(value);
                        descList.add(descView);
                    }
                }
                list.addAll(descList);
            }
            for (int i = 0; i < configsArray.length(); i++) {
                List<View> descList = new ArrayList<View>();
                JSONObject pJson = configsArray.getJSONObject(i);
                Iterator<?> keys = pJson.keys();
                while (keys.hasNext()) {
                    String key = (String) keys.next();
                    String value = pJson.get(key).toString();
                    if ("null".equals(value)) {
                        value = "";
                    }
                    if (key.equals("para_cat")) {
                        titleView = layoutInflater.inflate(R.layout.car_details_new_item_title, null);
                        txt = (TextView) titleView.findViewById(R.id.txt);
                        txt.setText(value);
                        list.add(titleView);
                    } else {
                        descView = layoutInflater.inflate(R.layout.car_details_new_item_desc, null);
                        title_txt = (TextView) descView.findViewById(R.id.title_txt);
                        desc_txt = (TextView) descView.findViewById(R.id.desc_txt);

                        title_txt.setText(key);
                        desc_txt.setText(value);
                        descList.add(descView);
                    }
                }
                list.addAll(descList);
            }
            for (View v : list) {
                new_car_details.removeView(v);
                new_car_details.addView(v);
            }

            if (paramsArray.length() <= 0 && configsArray.length() <= 0) {
                no_new_info.setVisibility(View.VISIBLE);
            } else {
                no_new_info.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            Utils.LogE("解析新车配置异常：" + e);
        }
    }
}
